/**
 * @fileoverview This file is the javascript involved for displaying the welcome/update pages upon installation.
 *
 * @author Jamie L. Vargas (2010/03/26)
 * Released under GPLv3. See file gpl-3.0.txt included in this package.
 */

var Prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
Prefs = Prefs.getBranch("extensions.odfview.");

var Overlay = {
  init: function(){
    var ver = -1;
    var firstrun = true;

    var gExtensionManager = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager);

    // get the version number.
    var current = gExtensionManager.getItemForID("{ff36e674-8438-4272-9046-2a7971a116e2}").version;

    try {
        ver = Prefs.getCharPref("version");
        firstrun = Prefs.getBoolPref("firstrun");

    } catch (e) {
        //nothing; proceed to finally clause

    } finally {

      Prefs.setBoolPref("firstrun",false);
      Prefs.setCharPref("version",current);

      // check if this is the first time installation
      if (firstrun) {
        // Load the welcome page
        setTimeout(function(){
            gBrowser.selectedTab = gBrowser.addTab("chrome://odfview/content/init/welcome.htm");
        }, 2000);
      }

      // check if this is an update to an existing installation
      if (ver!=current && !firstrun){ // !firstrun ensures that this section does not get loaded if it is a first run.
        Prefs.setCharPref("version",current);
        setTimeout(function(){
            gBrowser.selectedTab = gBrowser.addTab("chrome://odfview/content/init/update.htm");
        }, 2000);
      }

    } // end of finally clause

    window.removeEventListener("load",function(){ Overlay.init(); },true);
  }
};

window.addEventListener("load",function(){ Overlay.init(); },true);
